package xin.lz1998.cq.plugin.customize;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xin.lz1998.cq.constant.BannedConstant;
import xin.lz1998.cq.constant.TitleConstant;
import xin.lz1998.cq.event.message.CQGroupMessageEvent;
import xin.lz1998.cq.plugin.MinGanCi;
import xin.lz1998.cq.robot.CQPlugin;
import xin.lz1998.cq.robot.CoolQ;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 成员信息功能
 */
@Component
@Order(5)//插件执行顺序
public class BannedPlugin extends CQPlugin {

    private static boolean flag=false;


    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg=event.getMessage().trim() ;
        long groupId = event.getGroupId();
        long id = event.getUserId();

        if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.MINGANCION)&&BannedConstant.ADMINID == id) {
            flag = true;
            cq.sendGroupMsg(groupId, "已开启敏感词机制", true);
            cq.sendGroupMsg(groupId, "[CQ:face,id=14]", false);
        } else if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.MINGANCIOFF)&&BannedConstant.ADMINID == id) {
            flag = false;
            cq.sendGroupMsg(groupId, "已关闭敏感词机制", true);
            cq.sendGroupMsg(groupId, "[CQ:face,id=14]", false);
        }

        //敏感词
        if(flag) {
            String[] split = MinGanCi.minganci.split(" ");
            for (int i = 0; i < split.length; i++) {
                if (msg.contains(split[i]) && BannedConstant.ADMINID != id) {
                    cq.sendGroupMsg(groupId, "用户[" + id + "]:" + msg + "含有敏感词汇:" + split[i], true);
                    cq.sendGroupMsg(groupId, "用户[" + id + "]:" + BannedConstant.MINGAN, true);
                    cq.sendGroupMsg(groupId, "[CQ:face,id=14]", false);
                    cq.setGroupBan(groupId, id, 2 * 60);
                    break;
                }
            }
            if (msg.length() > 0 && (msg.startsWith("?") || msg.startsWith("？")) && BannedConstant.ADMINID != id) {
                cq.sendGroupMsg(groupId, "用户[" + id + "]" + BannedConstant.MIAOSHI, true);
                cq.sendGroupMsg(groupId, "[CQ:face,id=14]", false);
                cq.setGroupBan(groupId, id, 2 * 60);
            }
        }

        String[] arr = msg.split(" ");
        if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.HEARD)&&arr.length>1){
            if(arr.length==2) {
                if(arr[1].startsWith("[")){
                    String[] idArr = arr[1].split("=");
                    Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                    Long adminId = event.getUserId();
                    if(user_id==TitleConstant.ADMINID){
                        cq.setGroupSpecialTitle(groupId, adminId, BannedConstant.CURRENTHEARD, true);
                        cq.setGroupBan(groupId,adminId,5*60);
                    }else{
                        cq.setGroupBan(groupId,user_id,1*60);
                    }
                }else{
                    cq.sendGroupMsg(groupId, BannedConstant.INFO, true);
                }
            }
            if(arr.length==3){
                try {
                    if(arr[1].startsWith("[")){
                        String[] idArr = arr[1].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                        Long adminId = event.getUserId();
                        String time = arr[2];
                        Long times=0L;
                        try {
                            times= Long.valueOf(time);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                            cq.sendGroupMsg(groupId, BannedConstant.INFO, true);
                        }

                        if(adminId==TitleConstant.ADMINID){
                            cq.setGroupBan(groupId,user_id,times*60);
                        }else{
                            cq.sendGroupMsg(groupId, BannedConstant.QUNZHUINFO, true);
                        }
                    }else{
                        cq.sendGroupMsg(groupId, BannedConstant.QUNZHUINFO, true);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }else if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.UNHEARD)&&arr.length>1){
            if(arr.length==2){


//                cq.sendGroupMsg(groupId, "[CQ:image,file=1.jpg]", false);
//                cq.sendDiscussMsg(groupId, "[CQ:emoji,id=128513]", false);
                cq.sendGroupMsg(groupId, "[CQ:face,id=14]", false);

                try {
                    if(arr[1].startsWith("[")){
                        String[] idArr = arr[1].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                        Long adminId = event.getUserId();
                        cq.setGroupBan(groupId,user_id,0);
                    }else{

                        cq.setGroupSpecialTitle(groupId,event.getUserId(),TitleConstant.ZHUHEARD,true);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }else if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.LKAICHE)){
            int i = new Random().nextInt(9) + 1;
            cq.sendGroupMsg(groupId, "[CQ:image,file=b/"+i+".gif]", false);
        }else if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.KAICHE)){
            cq.sendGroupMsg(groupId, "[CQ:image,file=C175386EB57990BA375253528EEAE39B.jpg]", false);
        }
        return MESSAGE_IGNORE;
    }


    public static String timestamp2Date(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time * 1000));
    }
}




